<template>
  <div class="container">
    <div class="app-container">
      <!-- 树形组织架构 -->
      <el-tree :data="depts" default-expand-all :expand-on-click-node="false" :props="defaultProps">
        <template #default="{ data }">
          <el-row style="width: 100%;" type="flex" justify="space-between" align="middle">
            <el-col><span>{{ data.name }}</span></el-col>
            <el-col :span="4">
              <span class="tree-manager">{{ data.managerName }}</span>
              <!-- $event 实参 表示类型 -->
              <el-dropdown @command="operateDept(data.id, $event)">
                <!-- 显示区域内容 -->
                <span class="el-dropdown-link">
                  操作<i class="el-icon-arrow-down el-icon--right" />
                </span>

                <!-- 下拉菜单选项 -->
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item command="add">添加子部门</el-dropdown-item>

                  <el-dropdown-item command="edit">编辑部门</el-dropdown-item>

                  <el-dropdown-item command="del">删除</el-dropdown-item>

                </el-dropdown-menu>

              </el-dropdown>
            </el-col>
          </el-row>
        </template>
      </el-tree>
    </div>
    <!-- 添加/编辑部门 -->
    <addDept ref="addDept" :show-dialog.sync="showDialog" :current-id="currentId" @updateDepartment="getDepartment" />
    <!-- <addDept
      :show-dialog="showDialog"
      @update:showDialog="showDialog = $event"
    /> -->
  </div>
</template>
<script>
import { delDepartment, getDepartment } from '@/api/department'
import { transListToTreeData } from '@/utils'
import addDept from './componments/add-dept.vue'
export default {
  name: 'Department',
  components: {
    addDept
  },
  data() {
    return {
      depts: [],
      defaultProps: {
        label: 'name'
      },
      // 控制弹层的显示隐藏
      showDialog: false,
      currentId: null // 点击的部门id
    }
  },
  created() {
    this.getDepartment() // 调用获取数据的接口
  },
  methods: {
    // 操作下拉菜单部门
    operateDept(id, type) {
      console.log(type)
      if (type === 'add') {
        // 添加
        this.showDialog = true
        // 记录部门的id
        this.currentId = id
      } else if (type === 'edit') {
        // 调用子组件中的方法 - 获取部门信息
        // 父组件调用子组件中的方法 -> ref + $refs(1)获取dom - 精准(2)获取子组件的实例
        // console.log(this.$refs.addDept)TODO 问题 ->
        // 1. 更新子组件中的props数据是异步的
        // 2. 调用子组件实例身上的方法是同步的
        // 数据更新成功之后，再调用子组件的方法->等一等 $nextTick
        this.showDialog = true
        this.currentId = id
        this.$nextTick(() => {
          this.$refs.addDept.getDepartmentDetail() // this.$refs.addDept等同于子组件的this
        })
      } else {
        // 删除
        this.$confirm('您确定要删除该部门嘛').then(async() => {
          await delDepartment(id)
          // 提示消息
          this.$message.success('删除部门成功')
          this.getDepartment()
        })
      }
    },
    // 获取部门列表
    async getDepartment() {
      const result = await getDepartment()
      this.depts = transListToTreeData(result, 0)
    }
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  padding: 30px 140px;
  font-size: 14px;
}
.tree-manager {
  width: 80px;
  display: inline-block;
  margin: 10px;
}
</style>
